package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 * 衢州新闻网
 * @author Persh
 *
 */
public class Qz828NewsAnalyse implements AnalyseNews {
	
	private static final String[] regex={
		"http://news.qz828.com/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
		"http://house.qz828.com/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
		"http://health.qz828.com/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
		"http://auto.qz828.com/system/[0-9]{4}/[0-9]{2}/[0-9]{2}/[0-9]*.shtml",
	};
	
	
	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		news.setUrl(url);		
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1#itemtitle").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("div#ArticleTit").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("div#news_title").text();
				if(StringUtils.isBlank(title)){
					title =doc.select("div.text2 h1").text();
					if(StringUtils.isBlank(title)){
						title =doc.select("div#ItemTitle").text();
						if(StringUtils.isBlank(title)){
							title =doc.select("h1#artibodyTitle").text();
							if(StringUtils.isBlank(title)){
								Elements select=doc.select("div.htitle h1");
								if(select.size()>0){
									title=select.get(0).ownText();
								}
								if(StringUtils.isBlank(title)){
									title=doc.select("article h3").text();
								}
							}
						}
					}
				}
			}
		}
		content=HtmlCleaner.getContentHtml(url,doc.select("div#itemcon"));
		if(StringUtils.isBlank(content)){
			content=HtmlCleaner.getContentHtml(url,doc.select("div#ArticleCnt"));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url,doc.select("div#news_content"));
				if(StringUtils.isBlank(content)){
					content=HtmlCleaner.getContentHtml(url,doc.select("div.text2 p"));
					if(StringUtils.isBlank(content)){
						content=HtmlCleaner.getContentHtml(url,doc.select("div#ItemContent"));
						if(StringUtils.isBlank(content)){
							content=HtmlCleaner.getContentHtml(url,doc.select("div#artibody"));
							if(StringUtils.isBlank(content)){
								content=HtmlCleaner.getContentHtml(url,doc.select("div.largePic"));
								if(StringUtils.isBlank(content)){
									content=HtmlCleaner.getContentHtml(url,doc.select("div.content"));
								}
							}
						}
					}
				}
			}
		}
		date=DateUtils.matchDate(doc.select("div#itemintro").text());
		if(date==null){
			date=DateUtils.matchDate(doc.select("div#ArtFrom").text());
			if(date==null){
				date=DateUtils.matchDate(doc.select("div#news_datetime").text());
				if(date==null){
					date=DateUtils.matchDate(doc.select("div#ItemDatetime").text());
					if(date==null){
						date=DateUtils.matchDate(doc.select("div#articleInfo").text());
						if(date==null){
							date=DateUtils.matchDate(doc.select("div.htitle").text());
							if(date==null){
								date=DateUtils.matchDate(doc.select("article div.source-pubtime").text());
								if(date==null){
									date=DateUtils.matchDate(url);
								}
							}
						}
					}
				}
			}
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);	
		
		return news;
	}
	
	public static void main(String[] args) {
		Qz828NewsAnalyse analyse=new Qz828NewsAnalyse();
		String url="http://news.qz828.com/system/2013/09/27/010693972.shtml";
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
